/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/core/{{modules}}/service/{{entities@SQL}}Service.java.hbs
 */
package cn.ibizlab.core.ad.service;

import java.util.*;

import org.springframework.data.domain.Page;
import com.alibaba.fastjson.JSONObject;
import org.springframework.cache.annotation.CacheEvict;
import com.baomidou.mybatisplus.extension.service.IService;
import cn.ibizlab.util.security.SpringContextHolder;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.core.ad.domain.SysPerson;
import cn.ibizlab.core.ad.filter.SysPersonSearchContext;
import cn.ibizlab.core.ad.domain.SysDepartment;
import cn.ibizlab.core.ad.domain.SysOrganization;
import cn.ibizlab.core.open.domain.OpenAccessAuth;
import cn.ibizlab.core.ad.domain.SysMember;
import cn.ibizlab.core.ad.domain.SysCommonName;

/**
 * 人员服务接口[SysPersonService]
 *
 * @author generator
 */
public interface SysPersonService extends IService<SysPerson> {

    /**
     * 获取当前Service
     *
     * @return
     */
    default SysPersonService getSelf() {
        return SpringContextHolder.getBean(this.getClass());
    }

    /**
     * 获取
     * @param et
     * @return
     */
    SysPerson get(SysPerson et);
    /**
     * 获取
     * @param key
     * @return
     */
    default SysPerson get(String key) {
        return getSelf().get(new SysPerson().setId(key));
    }
    /**
     * id集合获取
     * @param ids
     * @return
     */
    default List<SysPerson> getByIds(Collection<String> ids) {
        List<SysPerson> entities =new ArrayList<>();
        ids.forEach(key -> entities.add(new SysPerson().setId(key)));
        return getSelf().getByEntities(entities);
    }
    /**
     * 对象集合获取
     * @param entities
     * @return
     */
    List<SysPerson> getByEntities(List<SysPerson> entities);

    /**
     * 草稿
     * @param et
     * @return
     */
    SysPerson getDraft(SysPerson et);

    /**
     * checkKey
     * @param et
     * @return
     */
    Integer checkKey(SysPerson et);

    /**
     * 创建
     * @param et
     * @return
     */
    boolean create(SysPerson et);
    /**
     * 批量创建
     * @param list
     * @return
     */
    @CacheEvict(value = "sysperson", allEntries = true)
    boolean createBatch(List<SysPerson> list);

    /**
     * 更新
     * @param et
     * @return
     */
    boolean update(SysPerson et);
    /**
     * 批量更新
     * @param list
     * @return
     */
    @CacheEvict(value = "sysperson", allEntries = true)
    boolean updateBatch(List<SysPerson> list);

    /**
     * 保存
     * @param et
     * @return
     */
    @Override
    boolean save(SysPerson et);
    /**
     * 批量保存
     * @param list
     * @return
     */
    @CacheEvict(value = "sysperson", allEntries = true)
    boolean saveBatch(List<SysPerson> list);

    /**
     * 主键删除
     * @param key
     * @return
     */
    default boolean remove(String key) {
        return getSelf().remove(new SysPerson().setId(key));
    }
    /**
     * 根据keys批量删除
     * @param keys
     * @return
     */
    default boolean remove(List<String> keys) {
        return removeBatch(keys);
    }
    /**
     * 根据对象删除
     * @param et
     * @return
     */
    @CacheEvict(value = "sysperson", key = "'row:'+#p0.id")
    boolean remove(SysPerson et);

    /**
     * 批量删除
     * @param ids
     * @return
     */
    default boolean removeBatch(Collection<String> ids) {
        List<SysPerson> entities =new ArrayList<>();
        ids.forEach(key -> entities.add(new SysPerson().setId(key)));
        return getSelf().removeByEntities(entities);
    }

    /**
     * 批量删除对象
     * @param entities
     * @return
     */
    boolean removeByEntities(List<SysPerson> entities);

    default ImportResult importData(String config, Boolean ignoreError, List<SysPerson> list) {
        ImportResult rt = new ImportResult().setTotal(list.size());
        for(int i=0;i<list.size();i++) {
            SysPerson et=list.get(i);
            try {
                this.getSelf().save(et);
                rt.setSuccess(rt.getSuccess()+1);
            }catch (Exception ex){
                rt.addItem(i+1,et.getDisplayName(),ex.getMessage());
                if(!ignoreError)
                    break;
            }
        }
        return rt;
    }

    /**
     * Activate
     * 
     * @param dto
     * @return
     */
    default SysPerson activate(SysPerson dto) {
        return dto;
    }

    /**
     * Cache_evict
     * 
     * @param dto
     * @return
     */
    default SysPerson cacheEvict(SysPerson dto) {
        return dto;
    }

    /**
     * Freeze
     * 
     * @param dto
     * @return
     */
    default SysPerson freeze(SysPerson dto) {
        return dto;
    }

    /**
     * GetSimple
     * 
     * @param key
     * @return
     */
    default SysPerson getSimple(String key) {
        return null;
    }

    /**
     * Info
     * 
     * @param key
     * @return
     */
    default SysPerson info(String key) {
        return null;
    }

    /**
     * Init
     * 为用户授予基础角色权限
     * @param dto
     * @return
     */
    default SysPerson init(SysPerson dto) {
        return dto;
    }

    /**
     * Items
     * 根据传入的organization_id 查询 organization_id对应单位的人员清单，其余可选过滤参数:department_id/department_id/organizational_role_id
     * @param dto
     * @return
     */
    default List<SysPerson> items(SysPerson dto) {
        return new ArrayList<>();
    }

    /**
     * ResetPassword
     * 
     * @param dto
     * @return
     */
    default SysPerson resetPassword(SysPerson dto) {
        return dto;
    }

    /**
     * RoleAuth
     * 授权系统角色
     * @param dto
     * @return
     */
    default SysPerson roleAuth(SysPerson dto) {
        return dto;
    }

    /**
     * searchCur
     * 
     * @param context
     * @return
     */
    Page<SysPerson> searchCur(SysPersonSearchContext context);
    /**
     * listCur
     * 
     * @param context
     * @return
     */
    List<SysPerson> listCur(SysPersonSearchContext context);

    /**
     * searchCurGroupMember
     * 
     * @param context
     * @return
     */
    Page<SysPerson> searchCurGroupMember(SysPersonSearchContext context);
    /**
     * listCurGroupMember
     * 
     * @param context
     * @return
     */
    List<SysPerson> listCurGroupMember(SysPersonSearchContext context);

    /**
     * searchDefault
     * 
     * @param context
     * @return
     */
    Page<SysPerson> searchDefault(SysPersonSearchContext context);
    /**
     * listDefault
     * 
     * @param context
     * @return
     */
    List<SysPerson> listDefault(SysPersonSearchContext context);

    /**
     * searchExternal
     * 
     * @param context
     * @return
     */
    Page<SysPerson> searchExternal(SysPersonSearchContext context);
    /**
     * listExternal
     * 
     * @param context
     * @return
     */
    List<SysPerson> listExternal(SysPersonSearchContext context);

    /**
     * 创建实体对象
     * @return
     */
    default SysPerson getEntity() {
        return new SysPerson();
    }
    /**
     * 创建搜索对象
     * @return
     */
    default SysPersonSearchContext getSearchContext() {
        return new SysPersonSearchContext();
    }
    /**
     * selectRelByMdepartmentId
     * @param mdepartmentIds
     * @return
     */
    List<SysPerson> findByMdepartmentId(List<String> mdepartmentIds);
    default List<SysPerson> findByMdepartmentId(String mdepartmentId) {
        return findByMdepartmentId(Arrays.asList(mdepartmentId));
    }
    /**
     * removeRelByMdepartmentId
     * @param mdepartmentId
     * @return
     */
    @CacheEvict(value = "sysperson", allEntries = true)
    boolean removeByMdepartmentId(String mdepartmentId);
    /**
     * resetRelByMdepartmentId
     * @param mdepartmentId
     * @return
     */
    @CacheEvict(value = "sysperson", allEntries = true)
    boolean resetByMdepartmentId(String mdepartmentId);
    /**
     * saveRelBySysDepartment
     * @param sysDepartment
     * @param list
     * @return
     */
    @CacheEvict(value = "sysperson", allEntries = true)
    boolean saveBySysDepartment(SysDepartment sysDepartment,List<SysPerson> list);
    /**
     * saveRelByMdepartmentId
     * @param mdepartmentId
     * @param list
     * @return
     */
    @CacheEvict(value = "sysperson", allEntries = true)
    default boolean saveByMdepartmentId(String mdepartmentId,List<SysPerson> list) {
        return getSelf().saveBySysDepartment(new SysDepartment().setId(mdepartmentId),list);
    }

    /**
     * selectRelByOrganizationId
     * @param organizationIds
     * @return
     */
    List<SysPerson> findByOrganizationId(List<String> organizationIds);
    default List<SysPerson> findByOrganizationId(String organizationId) {
        return findByOrganizationId(Arrays.asList(organizationId));
    }
    /**
     * removeRelByOrganizationId
     * @param organizationId
     * @return
     */
    @CacheEvict(value = "sysperson", allEntries = true)
    boolean removeByOrganizationId(String organizationId);
    /**
     * resetRelByOrganizationId
     * @param organizationId
     * @return
     */
    @CacheEvict(value = "sysperson", allEntries = true)
    boolean resetByOrganizationId(String organizationId);
    /**
     * saveRelByOrganization
     * @param sysOrganization
     * @param list
     * @return
     */
    @CacheEvict(value = "sysperson", allEntries = true)
    boolean saveByOrganization(SysOrganization sysOrganization,List<SysPerson> list);
    /**
     * saveRelByOrganizationId
     * @param organizationId
     * @param list
     * @return
     */
    @CacheEvict(value = "sysperson", allEntries = true)
    default boolean saveByOrganizationId(String organizationId,List<SysPerson> list) {
        return getSelf().saveByOrganization(new SysOrganization().setId(organizationId),list);
    }

    default List<SysMember> getMemberOf(SysPerson et) {
        return new ArrayList<>();
    }


    /**
     * 自定义查询SQL
     * @param sql  select * from table where id =#{et.param}
     * @param param 参数列表  param.put("param","1");
     * @return
     */
    List<JSONObject> select(String sql, Map<String,Object> param);

    /**
     * 自定义SQL
     * @param sql  update table  set name ='test' where id =#{et.param}
     * @param param 参数列表  param.put("param","1");
     * @return
     */
    boolean execute(String sql, Map<String,Object> param);

}